//
//  SmsCloudAearController.m
//  smsCloud
//
//  Created by 吴斌 on 2024/7/20.
#import "SmsCloudAearController.h"
#import "SmsCloudMainNavView.h"
#import "SmsCloudSearchSearchView.h"
#import "SmsCloudAearHistoryView.h"
#import "SmsCloudSearchAearResultView.h"
@interface SmsCloudAearController ()<UIGestureRecognizerDelegate>
@property (nonatomic,strong) SmsCloudMainNavView * smsCloudSearchNavView;
@property (nonatomic,strong) SmsCloudSearchSearchView * smsCloudSearchSearchView;
@property (nonatomic, strong)SmsCloudAearHistoryView * smsCloudAearHistoryView;
@property (nonatomic, strong)SmsCloudSearchAearResultView * smsCloudSearchAearResultView;

@property (nonatomic,copy) NSString *search;
@property (nonatomic, strong)NSArray * smsCloudAllAears;
@property (nonatomic, strong)NSMutableArray * smsCloudResultAears;
@end

@implementation SmsCloudAearController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"];
    [self smsCloudInitView];
    [self smsCloudGetAear];
}
- (SmsCloudMainNavView *)smsCloudSearchNavView{
    if(!_smsCloudSearchNavView){
        __weak __typeof__(self) weakSelf = self;
        _smsCloudSearchNavView = [[SmsCloudMainNavView alloc] init];
        _smsCloudSearchNavView.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"];
        _smsCloudSearchNavView.smsCloudNavTitle = @"区号查询";
        _smsCloudSearchNavView.SmsCloudMainBackBlock = ^{
            [weakSelf.navigationController popViewControllerAnimated:true];
        };
    }
    return _smsCloudSearchNavView;
}
- (SmsCloudSearchSearchView *)smsCloudSearchSearchView{
    if(!_smsCloudSearchSearchView){
        __weak __typeof__(self) weakSelf = self;
        _smsCloudSearchSearchView = [[SmsCloudSearchSearchView alloc] init];
        _smsCloudSearchSearchView.SmsCloudSearchBlock = ^(NSString * _Nonnull smsCloudSearch) {
            weakSelf.search = smsCloudSearch;
            [weakSelf smsCloudResult];
        };
    }
    return _smsCloudSearchSearchView;
}
- (SmsCloudAearHistoryView *)smsCloudAearHistoryView{
    if(!_smsCloudAearHistoryView){
        __weak __typeof__(self) weakSelf = self;
        _smsCloudAearHistoryView = [[SmsCloudAearHistoryView alloc] init];
        _smsCloudAearHistoryView.SmsCloudSearchBlock = ^(NSString * _Nonnull smsCloudSearch) {
            weakSelf.search = smsCloudSearch;
            weakSelf.smsCloudSearchSearchView.smsCloudSearchContent = smsCloudSearch;
            [weakSelf smsCloudResult];
        };
    }
    return _smsCloudAearHistoryView;
}
- (SmsCloudSearchAearResultView *)smsCloudSearchAearResultView{
    if(!_smsCloudSearchAearResultView){
        __weak __typeof__(self) weakSelf = self;
        _smsCloudSearchAearResultView = [[SmsCloudSearchAearResultView alloc] init];
    }
    return _smsCloudSearchAearResultView;
}
- (void)smsCloudInitView{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smsCloudEndEdit)];
    tap.delegate = self;
    [self.view addGestureRecognizer:tap];
    
    [self.view addSubview:self.smsCloudSearchNavView];
    [self.view addSubview:self.smsCloudSearchSearchView];
    [self.view addSubview:self.smsCloudAearHistoryView];
    [self.view addSubview:self.smsCloudSearchAearResultView];
    
    [self.smsCloudSearchNavView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:88] + [SmsCloudHead SmsCloudTopSafeH]));
    }];
    [self.smsCloudSearchSearchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(self.smsCloudSearchNavView.mas_bottom);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:125]));
    }];
    UIView * smsCloudLineView = [[UIView alloc] init];
    smsCloudLineView.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#000000" withAlpha:0.1];
    [self.view addSubview:smsCloudLineView];
    [smsCloudLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(self.smsCloudSearchSearchView.mas_bottom);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:1]));
    }];
    [self.smsCloudAearHistoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(smsCloudLineView.mas_bottom);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:170]));
    }];
    [self.smsCloudSearchAearResultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(self.smsCloudAearHistoryView.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:20]);
        make.bottom.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
    }];
}
- (void)smsCloudGetAear{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"SmsCloudAear" ofType:@"plist"];
    self.smsCloudAllAears = [NSArray arrayWithContentsOfFile:filePath];
    self.smsCloudResultAears = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"SmsCloudResultAear"]];
    if(!self.smsCloudResultAears){
        self.smsCloudResultAears = [[NSMutableArray alloc] init];
    }
    if(self.smsCloudResultAears.count > 0){
        [self.smsCloudAearHistoryView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:170]));
        }];
    }else{
        [self.smsCloudAearHistoryView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:110]));
        }];
    }
    self.smsCloudAearHistoryView.smsCloudResultAears = _smsCloudResultAears;
    
}
- (void)smsCloudResult{
    NSMutableArray * smsCloudResults = [[NSMutableArray alloc] init];
    if (self.search.length) {
        [self smsCloudEndEdit];
        //历史记录
        int flag = -1;
        for (int i = 0; i < self.smsCloudResultAears.count; i++) {
            NSString *smsCloudResultAear = self.smsCloudResultAears[i];
            if([smsCloudResultAear isEqualToString:self.search]){
                flag = i;
            }
        }
        if(flag == -1){
            [self.smsCloudResultAears insertObject:self.search atIndex:0];
        }else{
            [self.smsCloudResultAears removeObjectAtIndex:flag];
            [self.smsCloudResultAears insertObject:self.search atIndex:0];
        }
        [[NSUserDefaults standardUserDefaults] setObject:_smsCloudResultAears.copy forKey:@"SmsCloudResultAear"];
        //查询结果
        NSArray * products = self.smsCloudAllAears;
        for (int i = 0; i < products.count; i++) {
            NSDictionary *smsCloudProductDic = products[i];
            if ([[smsCloudProductDic objectForKey:@"name"] containsString:self.search]) {
                [smsCloudResults addObject:smsCloudProductDic];
            }
        }
    }
    if(self.smsCloudResultAears.count > 0){
        [self.smsCloudAearHistoryView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:170]));
        }];
    }else{
        [self.smsCloudAearHistoryView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:110]));
        }];
    }
    self.smsCloudAearHistoryView.smsCloudResultAears = _smsCloudResultAears;
    if (smsCloudResults.count <= 0) {
        [SVProgressHUD showInfoWithStatus:@"未找到结果"];
    }
    self.smsCloudSearchAearResultView.smsCloudSearchAearResults = smsCloudResults.copy;
}
- (void)smsCloudEndEdit{
    [self.view endEditing:true];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.smsCloudAearHistoryView.smsCloudHistoryCollectionView]) {
        // 允许在collection view单元格上进行手势
        return NO;
    }
    return YES;
}
@end
